﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Data;
using System.Web.Caching;
using System.Configuration;

public partial class Referal : System.Web.UI.Page
{
    private SqlConnection mySqlConnection;
    private SqlCommand mySqlCommand;
    private SqlDataAdapter mySqlDataAdapter;

    protected void Page_Load(object sender, EventArgs e)
    {
         if (!HttpContext.Current.Request.IsAuthenticated)
        {
            Response.Redirect("~/login.aspx");
        }
                      
    }

    private void CloseConnection()
    {
        try
        {
            if (mySqlConnection.State == ConnectionState.Open)
                mySqlConnection.Close();
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        catch (Exception ex2)
        {
            throw ex2;
        }
    }

    private void OpenConnection()
    {
        try
        {
            if (mySqlConnection.State != ConnectionState.Open)
                mySqlConnection.Open();
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        catch (Exception ex2)
        {
            throw ex2;
        }
    }

    private string GetConnectionString()
    {
        //return ConfigurationManager.ConnectionStrings["stockmarketConnectionString"].ConnectionString;
        return ConfigurationManager.ConnectionStrings["stockmarketConnectionString"].ConnectionString;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string userName = (string)Session["UserName"];
        string name = "";
        string referalCode="";

        try
        {
            mySqlConnection = new SqlConnection(GetConnectionString());
            mySqlCommand = mySqlConnection.CreateCommand();
            mySqlCommand.CommandText = "SELECT (FirstName + ' ( ' + LastName + ' )') AS Name, referalCode From [User] Where UserName=@UserName";
            mySqlCommand.CommandType = CommandType.Text;
            mySqlCommand.Parameters.AddWithValue("@UserName", userName);

            OpenConnection();

            mySqlCommand.ExecuteNonQuery();
            SqlDataReader myReader = mySqlCommand.ExecuteReader();
            if (myReader.Read())
            {
                name = (myReader["Name"].ToString());
                referalCode = (myReader["referalCode"].ToString());
            }

            CloseConnection();
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        catch (Exception ex2)
        {
            throw ex2;
        }


        result.Text = "";
        string emails = Email_TextBox.Text;
        emails = emails.Replace(" ", "");
        char[] seps = { ','};
        string[] values = emails.Split(seps);
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.live.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.Credentials = new System.Net.NetworkCredential("stockmarketsimulation@hotmail.com", "poiu1234");

        for (int i = 0; i < values.Length; i++)
        {
            try
            {
                smtp.Send(sendEmail((string)values[i], name, referalCode));

                result.Text += "<br><FONT COLOR='green'>'" + (string)values[i] + "' sent successfully. </font></br>"; 
            }
            catch (Exception ex)
            {
                result.Text += "<br><FONT COLOR='red'>'" + (string)values[i] + "' sent unsuccessfully. </font></br>";
            }
        }
    }

    private MailMessage sendEmail(string emailAddress, string FirstName, string referalCode)
    {
        MailMessage message = new System.Net.Mail.MailMessage();
        message.To.Add(emailAddress);
        message.Subject = "Referal from " + FirstName + " - Join me At Stock Market Simulation";
        message.From = new System.Net.Mail.MailAddress("stockmarketsimulation@hotmail.com");
        message.Body = @"Stock Market Simulation is fun! Join now and don't miss the fun. There is no worry on getting your fingers burned!" + Environment.NewLine +
                       "Visit http://localhost/SMSG/Registration.aspx and enter my referal code:'" + referalCode + "'";

        return message;
    }
}